//
//  RegisteredController.m
//  AntsExpress
//
//  Created by slccper on 16/1/6.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "RegisteredController.h"
#import "AntsPickView.h"
#import "RuleViewController.h"
@interface RegisteredController ()
{
    AntsPickView *Pickview;
    NSMutableArray *AreaList;
    NSMutableArray *WareList;
    NSMutableArray *BranchList;
    NSInteger areaSelect;
    NSInteger WareSelect;
    NSInteger BranchSelect;
}
@property (weak, nonatomic) IBOutlet UITextField *TFName;
@property (weak, nonatomic) IBOutlet UITextField *TFPwd;
@property (weak, nonatomic) IBOutlet UITextField *TFRePwd;
@property (weak, nonatomic) IBOutlet UITextField *TFNickName;
@property (weak, nonatomic) IBOutlet UITextField *TFPhone;
@property (weak, nonatomic) IBOutlet UITextField *TFSHPwd;
@property (weak, nonatomic) IBOutlet UIButton *AreaChose;
@property (weak, nonatomic) IBOutlet UIButton *Register;
@property (weak, nonatomic) IBOutlet UIButton *Warehouse;
@property (weak, nonatomic) IBOutlet UIButton *Address;
@property (weak, nonatomic) IBOutlet UIButton *Rule;
@property (weak, nonatomic) IBOutlet UIButton *Read;

@end

@implementation RegisteredController

- (void)viewDidLoad {
    [super viewDidLoad];
    ants = [[clsAntsService alloc]init];
    ants.delegate=self;
    AreaList = [NSMutableArray array];
    WareList = [NSMutableArray array];
    BranchList = [NSMutableArray array];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)AreaACtion:(id)sender {
    [self.view endEditing:YES];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            [self.AreaChose setTitle:pSelectData forState:UIControlStateNormal];
            areaSelect =select;
            self.AreaChose.selected=NO;
            self.Warehouse.selected=YES;
            [WareList removeAllObjects];
            self.Address.selected=YES;
        }];
        Pickview = nil;
    };
    if (AreaList.count>0) {
        NSMutableArray *title = [clsArea GetTitleByArr:AreaList];
        animations(title);
    }else{
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        [ants GetAreaList:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            AreaList = [clsArea GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsArea GetTitleByArr:AreaList];
            animations(title);
        }];
    }
}
- (IBAction)RegisterAction:(id)sender {
    [self.view endEditing:YES];
    NSString *MemberName = [self.TFName text];
    if ([MemberName isEqualToString:@""]||MemberName==nil) {
        [clsOtherFun ShowMsg:@"請輸入用戶名"];
        [self.TFName becomeFirstResponder];
        return;
    }
    NSString *MemberPwd = [self.TFPwd text];
    if ([MemberPwd isEqualToString:@""]||MemberPwd==nil) {
        [clsOtherFun ShowMsg:@"請輸入密碼"];
        [self.TFPwd becomeFirstResponder];
        return;
    }
    NSString *ReMemberPwd = [self.TFRePwd text];
    if ([ReMemberPwd isEqualToString:@""]||ReMemberPwd==nil) {
        [clsOtherFun ShowMsg:@"請輸入確認密碼"];
        [self.TFRePwd becomeFirstResponder];
        return;
    }
    if(![ReMemberPwd isEqualToString:MemberPwd])
    {
        [clsOtherFun ShowMsg:@"密碼與確認密碼不一致"];
        [self.TFRePwd becomeFirstResponder];
        return;
    }
    NSString *RealName = [self.TFNickName text];
//    if ([RealName isEqualToString:@""]||RealName==nil) {
//        [clsOtherFun ShowMsg:@"請輸入昵稱"];
//        [self.TFNickName becomeFirstResponder];
//        return;
//    }
    NSString *MemberPhone = [self.TFPhone text];
//    if ([MemberPhone isEqualToString:@""]||MemberPhone==nil) {
//        [clsOtherFun ShowMsg:@"請輸入電話"];
//        [self.TFPhone becomeFirstResponder];
//        return;
//    }
    NSString *ResivePwd = [self.TFSHPwd text];
    if (ResivePwd.length<6) {
        [clsOtherFun ShowMsg:@"收貨密碼不能少於六位！"];
        [self.TFSHPwd becomeFirstResponder];
        return;
    }
    if ([ResivePwd isEqualToString:@""]||ResivePwd==nil) {
        [clsOtherFun ShowMsg:@"請輸入收貨密碼"];
        [self.TFSHPwd becomeFirstResponder];
        return;
    }
    if (self.AreaChose.selected) {
        [clsOtherFun ShowMsg:@"請選擇區域"];
        return;
    }
    if (self.Warehouse.selected) {
        [clsOtherFun ShowMsg:@"請選擇倉庫"];
        return;
    }
    if (self.Address.selected) {
        [clsOtherFun ShowMsg:@"請選擇取貨點"];
        return;
    }
    if(!self.Read.selected){
        [clsOtherFun ShowMsg:@"請閱讀服務條款"];
        return;
    }
    clsArea *area =[AreaList objectAtIndex:areaSelect];
    clsWare *Ware  =[WareList objectAtIndex:WareSelect];
    clsBranch *Branch =[BranchList objectAtIndex:BranchSelect];
    NSString *remark = [NSString stringWithFormat:@"%@-%@-%@",self.AreaChose.titleLabel.text,self.Warehouse.titleLabel.text,self.Address.titleLabel.text];
    [ants Register:RealName :MemberName :MemberPwd :ResivePwd :MemberPhone :area.AreaID :Ware.WareID :Branch.BranchID :remark];
}
- (IBAction)Ware:(id)sender {
    [self.view endEditing:YES];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
                [self.Warehouse setTitle:pSelectData forState:UIControlStateNormal];
                WareSelect=select;
                self.Warehouse.selected=NO;
                [BranchList removeAllObjects];
                self.Address.selected=YES;
            }
            
        }];
        Pickview = nil;
    };
    if (WareList.count>0) {
        NSMutableArray *title = [clsWare GetTitleByArr:WareList];
        animations(title);
    }else{
        if (self.AreaChose.selected) {
            [clsOtherFun ShowMsg:@"請選擇區域"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        clsArea *area =[AreaList objectAtIndex:areaSelect];
        [ants GetWareList:area.AreaID action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            WareList = [clsWare GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsWare GetTitleByArr:WareList];
            animations(title);
        }];
    }
}
- (void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    if (type==Ants_Register) {
        NSDictionary *dic = [pDictionary objectForKey:@"data"];
        NSString *status = [dic objectForKey:@"status"];
        if ([status integerValue]==200) {
            [clsOtherFun ShowMsg:@"注册成功！"];
            [self .navigationController popViewControllerAnimated:YES];
        }else{
            [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
        }
    }
}
- (IBAction)addressAction:(id)sender {
    [self.view endEditing:YES];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
            [self.Address setTitle:pSelectData forState:UIControlStateNormal];
            self.Address.selected=NO;
            BranchSelect=select;
            }
        }];
        Pickview = nil;
    };
    if (BranchList.count>0) {
        NSMutableArray *title = [clsBranch GetTitleByArr:BranchList];
        animations(title);
    }else{
        if(self.Warehouse.selected){
            [clsOtherFun ShowMsg:@"請選擇倉庫"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        clsWare *area =[WareList objectAtIndex:WareSelect];
        [ants GetBranchList:area.WareID action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            BranchList = [clsBranch GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsBranch GetTitleByArr:BranchList];
            animations(title);
        }];
    }
}
- (IBAction)RuleAction:(id)sender {
    RuleViewController *rule = [[RuleViewController alloc]initWithNibName:@"RuleViewController" bundle:nil];
    rule.isBack =YES;
    [self.navigationController pushViewController:rule animated:YES];
}
- (IBAction)ReadAction:(id)sender {
    self.Read.selected=!self.Read.selected;
}

@end
